home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / beos / PPBeDevKit.ZIP / PLAYERPR.TAR / PlayerPRO / Source / Import-Export / Okta.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-26  |  13.9 KB  |  568 lines

  1. /********************                        ***********************/
  2. //
  3. //    Player PRO 5.0 - DRIVER SOURCE CODE -
  4. //
  5. //    Library Version 5.0
  6. //
  7. //    To use with MAD Library for Mac: Symantec, CodeWarrior and MPW
  8. //
  9. //    Antoine ROSSET
  10. //    16 Tranchees
  11. //    1206 GENEVA
  12. //    SWITZERLAND
  13. //
  14. //    COPYRIGHT ANTOINE ROSSET 1996, 1997, 1998
  15. //
  16. //    Thank you for your interest in PlayerPRO !
  17. //
  18. //    FAX:                (+41 22) 346 11 97
  19. //    PHONE:             (+41 79) 203 74 62
  20. //    Internet:     RossetAntoine@bluewin.ch
  21. //
  22. /********************                        ***********************/
  23.  
  24. #include "Okta.h"
  25. #include "MAD.h"
  26. #include "RDriver.h"
  27.  
  28. #if defined(powerc) || defined(__powerc)
  29. enum {
  30.         PlayerPROPlug = kCStackBased
  31.         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  32.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  33.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr)))
  34.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADMusic*)))
  35.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
  36.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( MADDriverSettings*)))
  37. };
  38.  
  39. ProcInfoType __procinfo = PlayerPROPlug;
  40. #else
  41. #include <A4Stuff.h>
  42. #endif
  43.  
  44. short decode16 (void *msg_buf)
  45. {
  46.   unsigned char *buf = msg_buf;
  47.   return ( (short) buf[0] << 8) | ( (short) buf[1]);
  48. }
  49.  
  50. unsigned long decode32 (void *msg_buf)
  51. {
  52.   unsigned char *buf = msg_buf;
  53.   
  54.   return( (unsigned long) buf[0] << 24) | ( (unsigned long) buf[1] << 16) | ( (unsigned long) buf[2] << 8) | ( (unsigned long) buf[3]);
  55. }
  56.  
  57. Ptr MADPlugNewPtr( long size, MADDriverSettings* init)
  58. {
  59.     if( init->sysMemory) return NewPtrSys( size);
  60.     else return NewPtr( size);
  61. }
  62.  
  63. Ptr MADPlugNewPtrClear( long size, MADDriverSettings* init)
  64. {
  65.     if( init->sysMemory) return NewPtrSysClear( size);
  66.     else return NewPtrClear( size);
  67. }
  68.  
  69. Cmd* GetMADCommand( register short PosX, register short    TrackIdX, register PatData*    tempMusicPat)
  70. {
  71.     if( PosX < 0) PosX = 0;
  72.     else if( PosX >= tempMusicPat->header.size) PosX = tempMusicPat->header.size -1;
  73.         
  74.     return( & (tempMusicPat->Cmds[ (tempMusicPat->header.size * TrackIdX) + PosX]));
  75. }
  76.  
  77. void pStrcpy(register unsigned char *s1, register unsigned char *s2)
  78. {
  79.     register short len, i;
  80.     
  81.     len = *s2;
  82.     for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
  83. }
  84. void mystrcpy( Ptr a, Ptr b)
  85. {
  86.     BlockMove( b + 1, a, b[ 0]);
  87. }
  88.  
  89. /*
  90. short FoundNote( short Period)
  91. {
  92. short    NCount = 1;
  93.     
  94.     while( NCount < NUMBER_NOTES+1)
  95.     {
  96.         if( Period >= MADpitchTable[ NCount]) return( NCount);
  97.         NCount++;
  98.     }
  99.     
  100.     return 0;
  101. }
  102. */
  103.  
  104. OSErr ConvertOKTA2Mad( Ptr    theOkta, long MODSize, MADMusic *theMAD, MADDriverSettings *init)
  105. {
  106.     short                 i, PatMax, x, z, channel, TrueTracks;
  107.     long                     sndSize, OffSetToSample, OldTicks, temp, starting;
  108.     Ptr                    MaxPtr, theOktaPos;
  109.     OSErr                theErr;
  110.     Ptr                    theInstrument[ 120], destPtr;
  111.     unsigned    short        tempS;
  112.     char                    tempChar;
  113.     
  114.     
  115.     /**** Variables pour le MAD ****/
  116.     Cmd                *aCmd;
  117.  
  118.     /**** Variables pour le Okta ****/
  119.     
  120.     OktaHeader        *Okta;
  121.     OktaInstru            *samps, *s, instru[ 120];
  122.     OktaPattern         *OktaCmd;
  123.     sectheader        *aSect;
  124.     long                SectLength;
  125.     short            pbod_count, sbod_count;
  126.     /********************************/
  127.  
  128.     for( i = 0 ; i < 64; i ++) theInstrument[ i] = 0L;
  129.  
  130.     theMAD->header = (MADSpec*) MADPlugNewPtrClear( sizeof( MADSpec), init);
  131.     Okta = ( struct OktaHeader*) MADPlugNewPtr( sizeof( struct OktaHeader), init);
  132.  
  133.     sbod_count = 0;
  134.     pbod_count = 0;
  135.     
  136.     MaxPtr        = theOkta + MODSize;
  137.     theOktaPos    = theOkta;
  138.     
  139.     if( (*(long*)theOkta) != 'OKTA') DebugStr("\pError in OKTA");
  140.     
  141.     theOktaPos += 8L;
  142.     
  143.     while( theOktaPos < MaxPtr)
  144.     {
  145.         aSect = ( sectheader*) theOktaPos;
  146.         aSect->length = decode32 (&aSect->length);
  147.         
  148.         theOktaPos += 8L;
  149.  
  150.         switch( aSect->name)
  151.         {
  152.             case 'CMOD':
  153.                 Okta->splitted[0] = theOktaPos[1];
  154.                       Okta->splitted[1] = theOktaPos[3];
  155.                       Okta->splitted[2] = theOktaPos[5];
  156.                       Okta->splitted[3] = theOktaPos[7];
  157.                       Okta->linesize = ((4 +Okta->splitted[0] + Okta->splitted[1]
  158.                                + Okta->splitted[2] + Okta->splitted[3])
  159.                               * 4);
  160.             break;
  161.             
  162.             case 'SAMP':
  163.                 samps = (void*) theOktaPos;
  164.                   
  165.                 for( i = 0; i * sizeof( OktaInstru) < aSect->length; i++)
  166.                         {
  167.                             instru[i] = samps[i];
  168.                             
  169.                     instru[ i ].length = decode32 (&instru[ i ].length);
  170.                     instru[ i ].repeat = decode16 (&instru[ i ].repeat) * 2;
  171.                     instru[ i ].replen = decode16 (&instru[ i ].replen) * 2;
  172.                 }
  173.                 Okta->samp_count = i;
  174.             break;
  175.  
  176.             case 'SPEE':
  177.                 Okta->speed = decode16( theOktaPos);
  178.             break;
  179.  
  180.             case 'SLEN':
  181.                 Okta->slen = decode16( theOktaPos);
  182.             break;
  183.  
  184.             case 'PLEN':
  185.                 Okta->plen = decode16( theOktaPos);
  186.             break;
  187.  
  188.             case 'PATT':
  189.                 Okta->patt = (void*)theOktaPos;
  190.             break;
  191.  
  192.             case 'PBOD':
  193.                 Okta->pbodlen[ pbod_count] = decode16 (theOktaPos);
  194.                 
  195.                 if( pbod_count == 0) theMAD->header->numChn = (aSect->length - 2L) / (Okta->pbodlen[ pbod_count] * 4L);
  196.                 else
  197.                 {
  198.                     if( theMAD->header->numChn != (aSect->length - 2L) / (Okta->pbodlen[ pbod_count] * 4L))
  199.                     {
  200.                         DebugStr("\pNon-standard OKTA - numChn");
  201.                     }
  202.                 }
  203.  
  204.                 Okta->pbod[ pbod_count++] = (theOktaPos + 2L);
  205.             break;
  206.  
  207.             case 'SBOD':
  208.                 s = &instru[ sbod_count];
  209.                 
  210.                 if (Okta->splitted[0] || Okta->splitted[1] || Okta->splitted[2] || Okta->splitted[3])
  211.                 {
  212.                 }
  213.                 
  214.                 s->length = aSect->length;        //if (s->length < aSect->length) 
  215.                 
  216.                 theInstrument[ sbod_count] = theOktaPos;
  217.                 
  218.                 if (s->replen == 0)
  219.                 {
  220.                 }
  221.                 else if (s->repeat != 0)
  222.                 {
  223.                 }
  224.                 else
  225.                 {
  226.                 }
  227.                 
  228.                 sbod_count++;
  229.             break;
  230.  
  231.             default:
  232.                 DebugStr("\pUnknow section");
  233.             break;
  234.         }
  235.         theOktaPos += aSect->length;
  236.     }
  237.  
  238.     /******** Le Okta a ÄtÄ lu et analysÄ ***********/
  239.     /******** Copie des informations dans le MAD ***/
  240.  
  241.     theMAD->header->MAD = 'MADI';
  242.     for(i=0; i<32; i++) theMAD->header->name[i] = 0;
  243.     for(i=0; i<20; i++) theMAD->header->name[i] = Okta->magic[i];
  244.     
  245.     theMAD->header->numPat         = pbod_count;
  246.     theMAD->header->numPointers     = pbod_count;
  247.     theMAD->header->tempo             = 125;
  248.     theMAD->header->speed             = Okta->speed;
  249.     
  250.     mystrcpy( theMAD->header->infos, (Ptr) "\pConverted by PlayerPRO OKTA Plug (⌐Antoine ROSSET <rossetantoine@bluewin.ch>)");
  251.     
  252.     for( i = 0;  i < 128; i++) theMAD->header->oPointers[ i] = 0;
  253.     for( i = 0;  i < pbod_count; i++) theMAD->header->oPointers[ i] = Okta->patt[ i];
  254.     
  255.     for( i = 0; i < MAXTRACK; i++)
  256.     {
  257.         if( i % 2 == 0) theMAD->header->chanPan[ i] = MAX_PANNING/4;
  258.         else theMAD->header->chanPan[ i] = MAX_PANNING - MAX_PANNING/4;
  259.         
  260.         theMAD->header->chanVol[ i] = MAX_VOLUME;
  261.     }
  262.     theMAD->header->generalVol        = 64;
  263.     theMAD->header->generalSpeed    = 80;
  264.     theMAD->header->generalPitch    = 80;
  265.  
  266.     
  267.     // INSTRUMENTS
  268.     
  269.     theMAD->fid = ( InstrData*) MADPlugNewPtrClear( sizeof( InstrData) * (long) MAXINSTRU, init);
  270.     if( !theMAD->fid) return MADNeedMemory;
  271.     
  272.     theMAD->sample = ( sData**) MADPlugNewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE, init);
  273.     if( !theMAD->sample) return MADNeedMemory;
  274.     
  275.     for( i = 0; i < MAXINSTRU; i++) theMAD->fid[ i].firstSample = i * MAXSAMPLE;
  276.  
  277.     
  278.     for(i=0; i< sbod_count; i++)
  279.     {
  280.         for( x = 0; x < 22; x++) theMAD->fid[i].name[x] = instru[i].name[ x];
  281.         theMAD->fid[i].type = 0;
  282.         
  283.         if( instru[i].length > 0)
  284.         {
  285.             sData    *curData;
  286.             
  287.             theMAD->fid[i].numSamples = 1;
  288.             theMAD->fid[i].volFade = DEFAULT_VOLFADE;
  289.             
  290.             curData = theMAD->sample[ i*MAXSAMPLE + 0] = (sData*) MADPlugNewPtrClear( sizeof( sData), init);
  291.             
  292.             curData->size        = instru[i].length;
  293.             curData->loopBeg     = instru[i].repeat;
  294.             curData->loopSize     = instru[i].replen;
  295.             curData->vol        = instru[i].vol;
  296.             curData->c2spd        = NOFINETUNE;
  297.             curData->loopType    = 0;
  298.             curData->amp        = 8;
  299.             
  300.             curData->relNote    = 0;
  301.             
  302.             curData->data         = MADPlugNewPtr( curData->size, init);
  303.             if( curData->data == 0L) return MADNeedMemory;
  304.                 
  305.             BlockMove( theInstrument[i], curData->data, curData->size);
  306.         }
  307.         else theMAD->fid[ i].numSamples = 0;
  308.     }
  309.     
  310.     //*** TEMPORAIRE *****
  311.     
  312.     TrueTracks = theMAD->header->numChn;
  313.     
  314.     theMAD->header->numChn /= 2;
  315.     theMAD->header->numChn *= 2;
  316.     if( theMAD->header->numChn != TrueTracks) theMAD->header->numChn += 2;
  317.     
  318.     for(i=0; i<MAXPATTERN;i++) theMAD->partition[ i] = 0L;
  319.     
  320.     for(i=0; i<theMAD->header->numPat; i++)
  321.     {
  322.         theMAD->partition[ i] = (PatData*) MADPlugNewPtrClear( sizeof( PatHeader) + theMAD->header->numChn * Okta->pbodlen[ i] * sizeof( Cmd), init);
  323.  
  324.         theMAD->partition[ i]->header.size = Okta->pbodlen[ i];
  325.         theMAD->partition[ i]->header.compMode = 'NONE';
  326.         for( x = 0; x < 20; x++) theMAD->partition[ i]->header.name[ x] = 0;
  327.         theMAD->partition[ i]->header.patBytes = 0L;
  328.         theMAD->partition[ i]->header.unused2 = 0L;
  329.     
  330.         MaxPtr = (Ptr) theMAD->partition[ i];
  331.         MaxPtr += sizeof( PatHeader) + theMAD->header->numChn * Okta->pbodlen[ i] * sizeof( Cmd);
  332.  
  333.         for( x = 0; x < Okta->pbodlen[ i]; x++)
  334.         {
  335.             for(z=0; z<theMAD->header->numChn; z++)
  336.             {
  337.                 aCmd = GetMADCommand( x, z, theMAD->partition[ i]);
  338.                 if( (Ptr) aCmd >= MaxPtr) Debugger();
  339.                 aCmd->note        = 0xFF;
  340.                 aCmd->ins            = 0;
  341.                 aCmd->cmd        = 0;
  342.                 aCmd->arg        = 0;
  343.                 aCmd->vol        = 0xFF;
  344.                 
  345.                 if( z < TrueTracks)
  346.                 {
  347.                     OktaCmd = (OktaPattern*) (Okta->pbod[i] + x * TrueTracks * 4L + 4L*z);
  348.  
  349.                     if( OktaCmd->b1 > 0)
  350.                     {
  351.                         aCmd->note =     OktaCmd->b1 - 1;
  352.                         aCmd->note += 24;
  353.                         
  354.                         if( aCmd->note < 0 || aCmd->note >= NUMBER_NOTES) aCmd->note = 0xFF;
  355.                         
  356.                         aCmd->ins  = OktaCmd->b2 + 1;
  357.                     }
  358.  
  359.                     switch( OktaCmd->b3)
  360.                     {
  361.                         case 31:
  362.                             if (OktaCmd->b4 <= 0x40)
  363.                             {
  364.                                 aCmd->vol = OktaCmd->b4 + 0x10;
  365.                             }
  366.                         break;
  367.                     
  368.                         case 25:
  369.                             OktaCmd->b4 &= 0xf;
  370.                                           if(    OktaCmd->b4 != 0)
  371.                                           {
  372.                                               aCmd->cmd = speedE;
  373.                                               aCmd->arg = OktaCmd->b4;
  374.                                           }
  375.                         break;
  376.                     }
  377.                     
  378.                 /*    if( i == 0 && x == 0 && z == 0)
  379.                     {
  380.                         aCmd->cmd        = speedE;
  381.                         aCmd->arg        = Okta->speed;
  382.                     }*/
  383.                 }
  384.             }
  385.         }
  386.     }
  387.  
  388.     DisposePtr( (Ptr) Okta);
  389.  
  390.     return noErr;
  391. }
  392.  
  393. OSErr ExtractOKTAInfo( PPInfoRec *info, Ptr theOkta, long MODSize)
  394. {
  395.     long        PatternSize;
  396.     short    i;
  397.     short    maxInstru;
  398.     short    tracksNo;
  399.     
  400.     /*** Signature ***/
  401.     
  402.     info->signature = 'OKTA';
  403.     
  404.     /*** Internal name ***/
  405.     
  406.     MADstrcpy( info->internalFileName, "\p");
  407.     
  408.     {
  409.     OktaInstru            *samps, *s, instru[ 120];
  410.     OktaPattern         *OktaCmd;
  411.     sectheader        *aSect;
  412.     long                SectLength;
  413.     short            pbod_count, sbod_count;
  414.     
  415.     short                 i, PatMax, x, z, channel, TrueTracks;
  416.     long                     sndSize, OffSetToSample, OldTicks, temp, starting;
  417.     Ptr                    MaxPtr, theOktaPos;
  418.     OSErr                theErr;
  419.     Ptr                    theInstrument[ 120], destPtr;
  420.     unsigned    short        tempS;
  421.     char                    tempChar;
  422.     
  423.     
  424.     sbod_count = 0;    pbod_count = 0;
  425.     
  426.     MaxPtr        = theOkta + MODSize;
  427.     theOktaPos    = theOkta;
  428.     
  429.     if( (*(long*)theOkta) != 'OKTA') DebugStr("\pError in OKTA");
  430.     
  431.     theOktaPos += 8L;
  432.     
  433.     while( theOktaPos < MaxPtr)
  434.     {
  435.         aSect = ( sectheader*) theOktaPos;
  436.         aSect->length = decode32 (&aSect->length);
  437.         
  438.         theOktaPos += 8L;
  439.  
  440.         switch( aSect->name)
  441.         {
  442.             case 'PBOD':
  443.                 pbod_count++;
  444.             break;
  445.             
  446.             case 'SBOD':
  447.                 sbod_count++;
  448.             break;
  449.         }
  450.         theOktaPos += aSect->length;
  451.     }
  452.     info->totalPatterns = info->partitionLength = pbod_count;
  453.     info->totalInstruments = sbod_count;
  454.     }
  455.     
  456.     MADstrcpy( info->formatDescription, "OKTA Plug");
  457.  
  458.     return noErr;
  459. }
  460.  
  461. OSErr TestOKTAFile( Ptr AlienFile)
  462. {
  463. long    *myOKTA = ( long*) AlienFile;
  464.  
  465. if( *myOKTA == 'OKTA') return   noErr;
  466. else return  MADFileNotSupportedByThisPlug;
  467. }
  468.  
  469. OSErr main( OSType order, char *AlienFileName, MADMusic *MadFile, PPInfoRec *info, MADDriverSettings *init)
  470. {
  471.     OSErr    myErr;
  472.     Ptr        AlienFile;
  473.     short    vRefNum, iFileRefI;
  474.     long    dirID, sndSize;
  475.     
  476. #ifndef powerc
  477.     long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  478. #endif
  479.  
  480.     c2pstr( AlienFileName);
  481.     
  482.     myErr = noErr;
  483.  
  484.     switch( order)
  485.     {
  486.         case 'IMPL':
  487.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  488.             if( myErr == noErr)
  489.             {
  490.                 GetEOF( iFileRefI, &sndSize);
  491.             
  492.                 // ** MEMORY Test Start
  493.                 AlienFile = MADPlugNewPtr( sndSize * 2L, init);
  494.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  495.                 // ** MEMORY Test End
  496.                 
  497.                 else
  498.                 {
  499.                     DisposePtr( AlienFile);
  500.                     
  501.                     AlienFile = MADPlugNewPtr( sndSize, init);
  502.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  503.                     if( myErr == noErr)
  504.                     {
  505.                         myErr = TestOKTAFile( AlienFile);
  506.                         if( myErr == noErr)
  507.                         {
  508.                             myErr = ConvertOKTA2Mad( AlienFile,  GetPtrSize( AlienFile), MadFile, init);
  509.                         }
  510.                     }
  511.                     DisposePtr( AlienFile);    AlienFile = 0L;
  512.                 }
  513.                 FSClose( iFileRefI);
  514.             }
  515.         break;
  516.         
  517.         case 'TEST':
  518.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  519.             if( myErr == noErr)
  520.             {
  521.                 sndSize = 1024L;
  522.                 
  523.                 AlienFile = MADPlugNewPtr( sndSize, init);
  524.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  525.                 else
  526.                 {
  527.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  528.                     myErr = TestOKTAFile( AlienFile);
  529.                     
  530.                     DisposePtr( AlienFile);    AlienFile = 0L;
  531.                 }
  532.                 FSClose( iFileRefI);
  533.             }
  534.         break;
  535.  
  536.         case 'INFO':
  537.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  538.             if( myErr == noErr)
  539.             {
  540.                 GetEOF( iFileRefI, &info->fileSize);
  541.                 sndSize = info->fileSize;
  542.                 AlienFile = MADPlugNewPtr( sndSize, init);
  543.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  544.                 else
  545.                 {
  546.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  547.                     if( myErr == noErr)
  548.                     {
  549.                         myErr = ExtractOKTAInfo( info, AlienFile, sndSize);
  550.                     }
  551.                     DisposePtr( AlienFile);    AlienFile = 0L;
  552.                 }
  553.                 FSClose( iFileRefI);
  554.             }
  555.         break;
  556.         
  557.         default:
  558.             myErr = MADOrderNotImplemented;
  559.         break;
  560.     }
  561.  
  562.     p2cstr( (unsigned char*) AlienFileName);
  563.  
  564.     #ifndef powerc
  565.         SetA4( oldA4);
  566.     #endif
  567.     return myErr;
  568. }